// pages/order/order.js

const app = getApp()
const util = require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    menuList: [
      {
        txt: '全部订单',
      },
      {
        txt: '待支付',
      },
      {
        txt: '待发货',
      },
      {
        txt: '已发货',
      },
      {
        txt: '已完成',
      },
      {
        txt: '已取消',
      }
     
    ],
    proimgUrl: '',
    list: [],
    listHeight: '',
    timeState: true,
    payStateTime: false,
    title:'全部订单'
  },

  /**
   * 生命周期函数--监听页面加载
   */



  // 确认收货
  que:function(e){
    var that = this;
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: 'https://rt.gzziyu.com/mobile/ziyu.php?Action=querenshouhuo',
      data:{
        openid:app.globalData.opid,
        orderid: e.currentTarget.dataset.id
      },
      success:function(res){
        console.log(res)

       if(res.data.success == 1){
         wx.showToast({
           title: '确认收货成功',
           icon: 'success',
           duration: 1000
         })
         setTimeout(function(){
           that.order()
         },1000)

        
       }
      }
    })
  },
  // 去支付
  toOrderDetails: function (e) {
    let that = this
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    if (e.currentTarget.dataset.state == '待支付') {
      wx.request({
        url: that.data.url + 'ny_flow_pay.php?action=order_pre_wxpay',
        data: {
          order_id: e.currentTarget.dataset.id,
          openid: app.globalData.opid
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          wx.requestPayment(
            {
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr,
              'package': res.data.package,
              'signType': 'MD5',
              'paySign': res.data.paySign,
              'success': function (res) {
              },
              'fail': function (res) { },
              'complete': function (res) { }
            })
          wx.hideLoading()
        }
      })
    } else {
      wx.hideLoading()
      wx.navigateTo({
        url: './../order_detail/order_detail?id=' + e.currentTarget.dataset.id
      })
    }

  },
  // 去换货详情
  go_huan:function(e){
    var that= this;
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../progress/progress?id=' + e.currentTarget.dataset.id,
    })
  },
  order: function (type) {
    let that = this
    var type = type || ''
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let order = util.wxPromisify(wx.request)
    order({
      url: that.data.url + 'ziyu.php?Action=order_list',
      data: {
        openid: app.globalData.opid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      }
    }).then((res) => {
      let timeState = that.data.timeState;
      console.log(type,timeState)
      if (type == 1 && timeState) {
        setInterval(() => {
          that.times()
        }, 1000)
      }
      console.log(res)
      that.setData({
        list: res.data
      })

      var list = that.data.list
      setTimeout(function () {
        that.getHeight(list)
        wx.hideLoading()
      }, 1000)

    })
    // wx.request({
    //
    //     success: function (res) {
    //         that.setData({
    //             list: res.data
    //         })
    //         that.getHeight()
    //         wx.hideLoading()
    //     }
    // })
  },
  // 点击标题
  active: function (e) {
    let menuList = this.data.menuList
    let idx = e.currentTarget.dataset.index
    let title = e.currentTarget.dataset.title;
    let that = this
    menuList.map((item, i) => {
      item.active = false
    })
    menuList[idx].active = !menuList[idx].active
    that.setData({
      menuList: menuList
    });

    // (idx === 5) ? this.all() : this.order(idx+1)

    that.setData({
      title:title
    });


  },
  haun:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../transfer_application/transfer_application?orderid=' + e.currentTarget.dataset.id
    })
  },
  all: function (e) {
    let that = this
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let all = util.wxPromisify(wx.request)

    all({
      url: that.data.url + 'ziyu.php?Action=order_list',
      data: {
        openid: app.globalData.opid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
    }).then((res) => {
      console.log(res)
      that.setData({
        list: res.data
      })
      let timeState = that.data.timeState
      if (timeState) {
        setInterval(() => {
          that.times()
        }, 1000)
      }


      var list = that.data.list
      setTimeout(function () {
        that.getHeight(list)
        wx.hideLoading()
      }, 1000)

    })
    // wx.request({
    //
    //     success: function (res) {
    //
    //     }
    // })
  },


  times: function () {


    this.setData({
      timeState: false
    })

    let list = this.data.list
    let killState = this.data.killState

    list.map((item, i) => {
      // let minute = item.surplus_time_minute
      // let second = item.surplus_time_second
      if (item.surplus_time_minute <= 0 && item.surplus_time_second <= 1) {
        item.payStateTime = true
        this.setData({
          list: list
        })
        let menuList = this.data.menuList
        menuList[0].active = !menuList[0].active
        this.all()

      }
      if (item.surplus_time_second) {
        if (Number(item.surplus_time_second) <= 0 && item.surplus_time_minute <= 0) {
          this.setData({
            killState: false
          })
        }

        item.surplus_time_second--

        if (item.surplus_time_second < 10) {
          item.surplus_time_second = '0' + item.surplus_time_second
        }

        this.setData({
          list: list,
        })

        if (item.surplus_time_second == 0 && item.surplus_time_minute == 0) {
          this.setData({
            killState: false,
          })
        }
        if (item.surplus_time_second == 0) {
          item.surplus_time_minute--

          this.setData({
            list: list,
          })

          item.surplus_time_second = 60

          if (item.surplus_time_second < 10) {
            item.surplus_time_second = '0' + item.surplus_time_second
          }
          this.setData({
            list: list,
          })
        }
        this.setData({
          list: list,

        })
      }

    })

  },



  // 取消订单
  cancel: function (e) {
    let that = this
    wx.request({
      url: app.globalData.url + 'ny_flow_cart.php?action=cancel_order',
      data: {
        openid: app.globalData.opid,
        order_id: e.currentTarget.dataset.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        that.all()
        wx.showToast({
          title: '取消成功',
          icon: 'success',
          mask: true,
          duration: 2000
        })
      }
    })
  },
  show: function (e) {
    let list = this.data.list
    let index = e.currentTarget.dataset.index
    let length = list[index].goods_list.length
    let listHeight = JSON.parse(JSON.stringify(list[index].listHeight))

    list[index].showState = !list[index].showState

    if (list[index].showState) {
      list[index].listHeight = list[index].height * length / 2
    } else {
      list[index].listHeight = list[index].height * 1
    }

    this.setData({
      list: list
    })
  },
  onLoad: function (options) {
    let that = this
    let proimgUrl = app.globalData.url.replace('mobile/', '')

    this.setData({
      userInfo: app.globalData.userInfo,
      url: app.globalData.url,
      proimgUrl: proimgUrl

    })

    if(options.id){
      switch (options.id) {
        case '0':
          that.setData({
            title: "全部订单",

          })
          break
        case '1':
          that.setData({
            title: "待支付",

          })
          break
        case '2':
          that.setData({
            title: "待发货"
          })
          break
        case '3':
          that.setData({
            title: "已发货"
          })
          break
        case '4':
          that.setData({
            title: "已完成"
          })
          break
        case '5':
          that.setData({
            title: " "
          })
          break
      }
    }
    console.log(options)

    let menuList = this.data.menuList
   
    if (options.id != undefined && options.id != '0') {
      var id = options.id;
      // if(options.id == '5'){
      //   id = 4
      // }

      menuList[id].active = !menuList[id].active;
      this.order(id)
      this.setData({
        menuList: menuList
      })
    } else {
      menuList[0].active = !menuList[0].active
      this.all(options.id)
      this.setData({
        menuList: menuList
      })
    }

  },


  getHeight: function (list) {
    var listHeight = this.data.listHeight
    var query = wx.createSelectorQuery();//选择id
    var that = this
    // query.select('#list').boundingClientRect()
    // query.exec(function (res) {
    //   list.map((item, i) => {
    //     item.listHeight = res[0].height
    //     item.height = res[0].height
    //   })
    //   if (res[0].height) {
    //     that.setData({
    //       list: list,
    //     })
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */

  onUnload: function () {
    this.gotoHomePage();
   
  },
  gotoHomePage:function(){
    var pages = getCurrentPages() //获取加载的页面

    var currentPage = pages[pages.length - 2] //获取当前页面的对象

    var url = currentPage.route //当前页面url;
    if (currentPage != 'pages/index/index'){
      wx.reLaunch({
        url: '../index/index'
      })
    }
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})